package com.zyms.service.impl;

import com.zyms.myRunTime.MyRunTimeExcption;
import com.zyms.service.Base.BaseService;
import com.zyms.service.ProducerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Baggio on 2018-1-30.
 */
@Service
public class ProducerServiceImpl extends BaseService implements ProducerService {
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendQueue(String exchange_key, String queue_key, Object object) {
        logger.info("sendQueue.....");
        // convertAndSend 将Java对象转换为消息发送至匹配key的交换机中Exchange
        if(StringUtils.isBlank(queue_key)){
            queue_key="queueSMSKey";
        }
        amqpTemplate.convertAndSend(exchange_key, queue_key, object);
    }

    @Override
    public void sendQueue(Message message) throws MyRunTimeExcption {
        amqpTemplate.send(message);
    }

    @Override
    public void sendDataToQueue(String queueKey, Object object) throws MyRunTimeExcption {
        try {
            logger.info("=========发送消息开始=============消息：" + object.toString());
            amqpTemplate.convertAndSend(queueKey, object);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
